package com.peas.platform.module.icims.expenditure.expenses.domain.impl;

import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.enums.CommonYesNo;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.expenditure.expenses.adapter.repository.ExpenditureApplicationRepository;
import com.peas.platform.module.icims.expenditure.expenses.adapter.repository.po.ExpenditureApplicationPo;
import com.peas.platform.module.icims.expenditure.expenses.domain.ExpenditureApplicationDomain;
import com.peas.platform.module.icims.expenditure.expenses.dto.ExpenditureApplicationDto;
import com.peas.platform.module.icims.expenditure.expenses.dto.ExpenditureApplicationFileDto;
import com.peas.platform.module.icims.expenditure.expenses.service.ExpenditureApplicationFileService;
import com.peas.platform.module.icims.expenditure.kpis.dto.ExpenditureKpiDto;
import com.peas.platform.module.icims.expenditure.kpis.service.ExpenditureKpiService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class ExpenditureApplicationDomainImpl extends BaseDomainImpl<ExpenditureApplicationDto, ExpenditureApplicationPo> implements ExpenditureApplicationDomain {

    @Resource
    private ExpenditureApplicationRepository expenditureApplicationRepository;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private ExpenditureApplicationFileService expenditureApplicationFileService;
    @Resource
    private ExpenditureKpiService expenditureKpiService;

    @Override
    public BaseRepository<ExpenditureApplicationPo> getMapper() {
        return expenditureApplicationRepository;
    }

    @Override
    public Class<? extends BaseRepository<ExpenditureApplicationPo>> getRepositoryClass() {
        return ExpenditureApplicationRepository.class;
    }

    @Transactional
    @Override
    public int save(ExpenditureApplicationDto dto) {
        if (StrUtil.isBlank(dto.getApplicationCode())) {
            String code = sequenceService.genSequence("expenditure", "ExpenditureApplicationDto", "EAD", 4, 1);
            dto.setApplicationCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("applicationCode", dto.getApplicationCode());
        //回写指标单
        ExpenditureKpiDto kpi = expenditureKpiService.getById(dto.getExpenditureKpiId());
        if (!StrUtil.equals(CommonYesNo.yes.getKey(), kpi.getIsBegin())) {
            kpi.setIsBegin(CommonYesNo.yes.getKey());
            expenditureKpiService.updateById(kpi);
        }
        return result;
    }

    @Transactional
    @Override
    public int updateById(ExpenditureApplicationDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("applicationCode", dto.getApplicationCode());
        //回写指标单
        ExpenditureKpiDto kpi = expenditureKpiService.getById(dto.getExpenditureKpiId());
        if (!StrUtil.equals(CommonYesNo.yes.getKey(), kpi.getIsBegin())) {
            kpi.setIsBegin(CommonYesNo.yes.getKey());
            expenditureKpiService.updateById(kpi);
        }
        return result;
    }

    @Transactional
    @Override
    public int removeById(Serializable id) {
        int result = super.removeById(id);
        List<ExpenditureApplicationFileDto> list = expenditureApplicationFileService.listByField("expenditureApplicationId", id);
        expenditureApplicationFileService.removeBatchByList(list);
        return result;
    }

}
